﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace CollectionAndShare
{
    /// <summary>
    /// Picture.xaml 的交互逻辑
    /// </summary>
    public partial class Picture : UserControl
    {
        public Picture()
        {
            InitializeComponent();
                     
            List<PictureObject> listPicObj = PictureObject.CreateCollections();

            int groupCount = Convert.ToInt32(listPicObj.Count / 12);

            List<PicTemplate> listPicTemplate = new List<PicTemplate>();

            PictureObject[] picObjArr = listPicObj.ToArray();

            listPicObj = null;

            for(int i=0; i<groupCount; i++)
            {                
                PicTemplate picTemp = new PicTemplate();
                picTemp.Icon0 = picObjArr[0 + i * 12].Icon;
                picTemp.Icon1 = picObjArr[1 + i * 12].Icon;
                picTemp.Icon2 = picObjArr[2 + i * 12].Icon;
                picTemp.Icon3 = picObjArr[3 + i * 12].Icon;
                picTemp.Icon4 = picObjArr[4 + i * 12].Icon;
                picTemp.Icon5 = picObjArr[5 + i * 12].Icon;
                picTemp.Icon6 = picObjArr[6 + i * 12].Icon;
                picTemp.Icon7 = picObjArr[7 + i * 12].Icon;
                picTemp.Icon8 = picObjArr[8 + i * 12].Icon;
                picTemp.Icon9 = picObjArr[9 + i * 12].Icon;
                picTemp.Icon10 = picObjArr[10 + i * 12].Icon;
                picTemp.Icon11 = picObjArr[11 + i * 12].Icon;
                listPicTemplate.Add(picTemp);
            }

            //if (listPicObj.Count % 12 != 0)
            //{
            //    PicTemplate picTemp = new PicTemplate();
            //    picTemp.Icon0 = "";
            //    picTemp.Icon1 = "";
            //    picTemp.Icon2 = "";
            //    picTemp.Icon3 = "";
            //    picTemp.Icon4 = "";
            //    picTemp.Icon5 = "";
            //    picTemp.Icon6 = "";
            //    picTemp.Icon7 = "";
            //    picTemp.Icon8 = "";
            //    picTemp.Icon9 = "";
            //    picTemp.Icon10 = "";
            //    picTemp.Icon11 = "";
            //}

            this.lstPicture.ItemsSource = listPicTemplate;
        }
    }

    public class PicTemplate
    {
        public string Icon0 { get; set; }
        public string Icon1 { get; set; }
        public string Icon2 { get; set; }
        public string Icon3 { get; set; }
        public string Icon4 { get; set; }
        public string Icon5 { get; set; }
        public string Icon6 { get; set; }
        public string Icon7 { get; set; }
        public string Icon8 { get; set; }
        public string Icon9 { get; set; }
        public string Icon10 { get; set; }
        public string Icon11 { get; set; }
    }

    public class PictureObject
    {
        public string Icon { get; set; }

        public string Name { get; set; }

        public string Date { get; set; }

        //public double Width { get; set; }

        //public double Hight { get; set; }

        public PictureObject(string icon, string name, string date)
        {
            this.Icon = icon;
            this.Name = name;
            this.Date = date;
            //this.Width = width;
            //this.Hight = height;
        }

        public static List<PictureObject> CreateCollections()
        {

            string[] imgExtension = { ".jpg",".jpeg",".png",".gif",".bmp"};
            string picFolder = AppDomain.CurrentDomain.BaseDirectory + "Source\\Picture\\";

            List<PictureObject> listCollection = new List<PictureObject>();

            if (Directory.Exists(picFolder))
            { 
                string[] files = Directory.GetFiles(picFolder);
                foreach(string file in files)
                {
                    if (File.Exists(file))
                    {
                        FileInfo fileInfo = new FileInfo(file);
                        if (imgExtension.Contains(fileInfo.Extension))
                        {
                            //Uri uri = new Uri(file, UriKind.Absolute);
                            //BitmapImage bitMap = new BitmapImage(uri);
                            //double width = bitMap.Width;
                            //double height = bitMap.Height;
                            PictureObject collection = new PictureObject(file, "", DateTime.Now.ToShortDateString());
                            listCollection.Add(collection);
                        }
                    }
                }
            }

            return listCollection;
            
        }


    }
}
